package com.cskaoyan.service.wx.search;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.bo.wx.search.WxSearchIndexVo;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.utils.GetUserInfoUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author erdonc
 * @date 2022/9/9 15:42
 * @description: TODO
 */
@Service
public class WxSearchServiceImpl implements WxSearchService {

    @Autowired
    MarketSearchHistoryMapper historyMapper;
    @Autowired
    MarketKeywordMapper keywordMapper;


    /**
     * @description: TODO 点击搜索框，显示热搜，和历史记录
     * @date: 2022/9/9 16:22
     * @param: []
     */
    @Override
    public WxSearchIndexVo index() {
        MarketKeywordExample keywordExample1 = new MarketKeywordExample();
        MarketKeywordExample.Criteria keywordExampleCriteria1 = keywordExample1.createCriteria();
        keywordExampleCriteria1.andDeletedEqualTo(false).andIsDefaultEqualTo(true);

        MarketKeywordExample keywordExample2 = new MarketKeywordExample();
        MarketKeywordExample.Criteria keywordExampleCriteria2 = keywordExample2.createCriteria();
        keywordExampleCriteria2.andDeletedEqualTo(false).andIsHotEqualTo(true);

        // 校验是否为登录状态，只有登录状态下，才根据用户id查找历史搜索记录，没有登录只显示热搜和关键词
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        List<MarketSearchHistory> searchHistoryList = null;
        if (principals != null) {
            MarketUser user = (MarketUser) principals.getPrimaryPrincipal();

            MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
            historyExample.setOrderByClause("update_time desc");
            MarketSearchHistoryExample.Criteria historyExampleCriteria = historyExample.createCriteria();
            historyExampleCriteria.andDeletedEqualTo(false).andUserIdEqualTo(user.getId());
            // 接收查询到的搜索历史
            searchHistoryList = historyMapper.selectByExample(historyExample);
        }

        List<MarketKeyword> marketDefaultKeywords = keywordMapper.selectByExample(keywordExample1);
        List<MarketKeyword> marketHotKeywords = keywordMapper.selectByExample(keywordExample2);

        // 对查询到的数据进行封装,并返回
        WxSearchIndexVo wxSearchIndexVo = new WxSearchIndexVo();
        wxSearchIndexVo.setDefaultKeyword(marketDefaultKeywords.get(0));
        wxSearchIndexVo.setHistoryKeywordList(searchHistoryList);
        wxSearchIndexVo.setHotKeywordList(marketHotKeywords);
        return wxSearchIndexVo;
    }


    /**
     * @description: TODO 搜索框输入要搜索的商品关键字
     * @date: 2022/9/9 17:51
     * @param: []
     */
    @Override
    public List<String> helper(String inputWord) {
        ArrayList<String> arrayList = new ArrayList<>();

        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
        List<MarketSearchHistory> searchHistoryList = historyMapper.selectByExample(example);

        // 判断用户是否登录，如果登录就显示历史搜索记录
        if (SecurityUtils.getSubject().isAuthenticated()) {
            // 获取用户id，只查询当前用户的搜索历史
            MarketUser marketUser = getMarketUser();
            Integer userId = marketUser.getId();
            criteria.andUserIdEqualTo(userId);
            // 根据输入的关键词模糊查询
            criteria.andDeletedEqualTo(false);
            criteria.andKeywordLike("%" + inputWord + "%");

            // 遍历searchHistoryList，把keyword放进arrayList中返回
            for (MarketSearchHistory searchHistory : searchHistoryList) {
                arrayList.add(searchHistory.getKeyword());
            }
        }

        // // TODO 然后把输入的关键字存入到数据库中，并且与该用户id相关联
        // MarketSearchHistory searchHistory = new MarketSearchHistory();
        // // 插入到数据库中之前先判断数据库中是否已存在这条记录，不存在就插入
        // searchHistory.setUserId(userId);
        // searchHistory.setKeyword(inputWord);
        // searchHistory.setAddTime(new Date());
        // searchHistory.setUpdateTime(null);
        // searchHistory.setDeleted(false);
        //
        // for (String historyWord : arrayList) {
        //     if (!historyWord.equals(inputWord)) {
        //         historyMapper.insertSelective(searchHistory);
        //     }
        // }
        return arrayList;
    }


    /**
     * @description: TODO 清空搜索历史记录
     * @date: 2022/9/9 20:17
     * @param: []
     */
    @Override
    public void clearhistory() {
        // 获取当前用户id
        MarketUser marketUser = getMarketUser();
        Integer userId = marketUser.getId();

        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false).andUserIdEqualTo(userId);

        // 逻辑删除
        List<MarketSearchHistory> searchHistoryList = historyMapper.selectByExample(example);
        for (MarketSearchHistory searchHistory : searchHistoryList) {
            searchHistory.setDeleted(true);
            historyMapper.updateByPrimaryKey(searchHistory);
        }
    }


    /**
     * @description: TODO 获取用户id的方法
     * @date: 2022/9/9 19:41
     * @param: []
     */
    private MarketUser getMarketUser() {
        MarketUser userInfo = GetUserInfoUtils.getUserInfo();
        return userInfo;
    }

}
